package tests;

import java.util.ArrayList;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.statistics.HistogramDataset;

import computation.Geometry;

import triangulation.DelaunayMapping;
import util.GeoTriangle;
import util.GeoVertex;
import util.STLBReader;
import util.STLTriangle;

/**
 * Create histograms of data from meshes
 * @author Po
 *
 */
public class HistogramViewer extends JFrame
{
	private ChartPanel mainChart;
	public HistogramViewer(String title)
	{
		super(title);
		mainChart = null;
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	}
	
	public void setChart(JFreeChart c)
	{
		mainChart = new ChartPanel(c);
		setContentPane(mainChart);
		pack();
	}
	
	public static JFreeChart createHistogramChart(String chartName, String xLabel, String yLabel, HistogramDataset dataset)
	{
		final JFreeChart chart = ChartFactory.createHistogram(
				chartName,
				xLabel,
				yLabel,
				dataset,
				PlotOrientation.VERTICAL,
				false,
				false,
				false);
		
		chart.setAntiAlias(true);
		chart.setTextAntiAlias(true);
		return chart;
	}
	
	public static void createHistogram_Strike(HistogramViewer viewer, ArrayList<STLTriangle> triangles, double laserStrike, double laserDip)
	{
		String chartName = "Distribution Histogram of Strike Measurements";
		String xLabel = "Strike (Degrees)";
		String yLabel = "Number of Triangles";

		// fill the array with all the strike values
		double[] strike = new double[triangles.size()];
		for (int i = 0; i < triangles.size(); i++)
		{
			STLTriangle t = triangles.get(i);
			double[] snd = Geometry.computeStrikeNDipAngle2(t.normal, laserStrike, laserDip);
			strike[i] = snd[0];
		}
		
		HistogramDataset dataset = new HistogramDataset();
		dataset.addSeries("H1", strike, 36, 0.0, 360.0);
		
		viewer.setChart(createHistogramChart(chartName, xLabel, yLabel, dataset));
	}
	
	public static void createHistogram_Dip(HistogramViewer viewer, ArrayList<STLTriangle> triangles, double laserStrike, double laserDip)
	{
		String chartName = "Distribution Histogram of Dip Measurements";
		String xLabel = "Dip (Degrees)";
		String yLabel = "Number of Triangles";

		// fill the array with all the strike values
		double[] dip = new double[triangles.size()];
		for (int i = 0; i < triangles.size(); i++)
		{
			STLTriangle t = triangles.get(i);
			double[] snd = Geometry.computeStrikeNDipAngle2(t.normal, laserStrike, laserDip);
			dip[i] = snd[1];
		}
		
		HistogramDataset dataset = new HistogramDataset();
		dataset.addSeries("H1", dip, 18, 0.0, 90.0);
		
		viewer.setChart(createHistogramChart(chartName, xLabel, yLabel, dataset));
	}
	
	public static void createHistogram_TriangleSize(HistogramViewer viewer, ArrayList<STLTriangle> triangles)
	{
		String chartName = "Distribution Histogram of Triangle Area";
		String xLabel = "Area";
		String yLabel = "Number of Triangles";
		
		// fill the array with all the areas of each triangle
		double[] areas = new double[triangles.size()];
		for (int i = 0; i < triangles.size(); i++)
		{
			STLTriangle t = triangles.get(i);
			double area = Geometry.triangleArea(t.v1, t.v2, t.v3);
			areas[i] = Math.log(area);
		}
		
		HistogramDataset dataset = new HistogramDataset();
		dataset.addSeries("H1", areas, 10);
		
		viewer.setChart(createHistogramChart(chartName, xLabel, yLabel, dataset));
	}

	public static void main(String[] args)
	{
		ArrayList<STLTriangle> triangles = STLBReader.read();
		
		HistogramViewer viewer = new HistogramViewer("Histogram Viewer");
//		createHistogram_Strike(viewer, triangles, 0, 0);
//		createHistogram_Dip(viewer, triangles, 0, 0);
		createHistogram_TriangleSize(viewer, triangles);
		viewer.setVisible(true);
	}
}
